from django.shortcuts import render,reverse
from django.http import HttpResponse
import re
#前台中间件验证
class LoginMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response
        # One-time configuration and initialization.

    def __call__(self, request):
        path = request.path
        #print('1')
        homeurl=[
            reverse('cart'),
            reverse('cart_add'),
            reverse('cart_update'),
            reverse('cart_del'),
            reverse('cart_clear'),
            reverse('order'),
            reverse('order_create'),
            reverse('order_pay'),
            reverse('member_order'),
            reverse('member'),
        ]

        print(request)
        res=str(request)
        print(res[19:-2:])
        if path in homeurl:
            if not request.session.get('VipUser',None):
                return HttpResponse('<script>alert("请先登录");location.href="/log/?next={url}";</script>'.format(url=res[19:-2:]))


        response = self.get_response(request)
        return response